package com.ai.sb.util;

import com.ai.sb.mobile.CrmpfPubInfo;
import com.ai.sb.mobile.Offer;
import com.ai.sb.mobile.OfferAttr;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Date:  2018/6/26
 * Time:  12:44
 * Email: siy@hmgsoft.com
 * Created by siy
 */
@Component
public class MobileUtil {
    private Logger logger = LoggerFactory.getLogger(MobileUtil.class);
    private String mobileFolwUrl;
    private String mobileFolwStaffId;
    private String mobileCode;
    private String telNum;
    private String offerId;

    public MobileUtil(String telNum,String offerId,String mobileFolwUrl,String mobileFolwStaffId,String mobileCode){
        this.telNum = telNum;
        this.offerId = offerId;
        this.mobileFolwUrl = mobileFolwUrl;
        this.mobileFolwStaffId = mobileFolwStaffId;
        this.mobileCode = mobileCode;
    }
    public MobileUtil(){

    }

    public boolean toFlow(){
        JSONObject qs = new JSONObject();
        JSONObject params= new JSONObject();
        qs.put("params",params);
        //offer
        List<Offer> offerList = new ArrayList<>();
        Offer offer = new Offer(offerId);
        //afferAttr
        List<OfferAttr> offerAttrList = new ArrayList<>();
        offerAttrList.add(new OfferAttr());
        offer.setOfferAttrList(offerAttrList);
        offerList.add(offer);
        params.put("offerList",offerList);
        //crmpfPubInfo
        CrmpfPubInfo crmpfPubInfo = new CrmpfPubInfo(mobileFolwStaffId);
        params.put("crmpfPubInfo",crmpfPubInfo);
        //userMobile
        params.put("userMobile",telNum);
        //转jsonString
        String jsonStr = qs.toJSONString();
        logger.info("请求流量下发参数，{}",jsonStr);
        RestTemplate restTemplate = new RestTemplate();
        //http请求头信息
        HttpHeaders headers = new HttpHeaders();
        //设置头类型为json类型
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.add("X-Trans-Id",this.getTransId(mobileCode));
        headers.add("X-Channel-Id",mobileCode);
        //设置请求内容
        HttpEntity<String> strEntity = new HttpEntity<String>(jsonStr,headers);
        //获取post请求内容
        JSONObject json = null;
        try {
            json = restTemplate.postForEntity(mobileFolwUrl, strEntity, JSONObject.class).getBody();
            String rtnCode = (String) json.get("rtnCode");
            logger.info("流量下发结果，{}",json.toJSONString());
            if("0".equals(rtnCode)){
                Map map = (Map) json.get("object");
                if("0".equals(map.get("respCode"))){
                    return true;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }catch (Exception e){
            logger.info("流量下发远程请求报错");
            e.printStackTrace();
            return false;
        }
    }

    private String getTransId(String strCode){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String s = sdf.format(date);
        String num = (int)((Math.random()*9+1)*10000000)+"";
        String rs = strCode;
        rs+=s;
        rs+=num;
        return rs;
    }

}
